#!/usr/bin/env python

from datetime import datetime
from datetime import timedelta

TIME_SUFFIXES = ['s','m','h','D']
def convert_to_seconds(input_date):
    """
    This function tries to convert date custom format into seconds
    Example:
    - 24H -> 86400 seconds
    - 1D  -> 86400 seconds
    - 12H -> 43200 seconds
    - 60m -> 3600  seconds
    - 1M  -> 1D * 31
    """
    def get_multiplier(measure):
        if measure == 's':
            return 1
        elif measure == 'm':
            return 60
        elif measure == 'h':
            return 60*60
        elif measure == 'D':
            return 60*60*24
        else:
            return 1

    
    if not input_date:
        return (3600,"Empty input,defaults to 3600 seconds")


    result_log=''
    input_date = input_date.strip()
    time_measure = input_date[-1] # get last elem
    if time_measure not in TIME_SUFFIXES:
        time_measure = 's'
    multiplier = get_multiplier(time_measure)
    try:
        date = int(input_date[:-1]) * multiplier
        return (date,result_log)
    except ValueError:
        result_log =  "Bad input format, defaults to 1 hour (3600 seconds)"
        return (3600,result_log)

def calculate_time_ranges(input_date):
    """
    The functions calculates (from,to) date ranges
    """
    DATE_FORMAT ="YYYY-MM-DD HH24:MI:SS"

    if not input_date:
        return (3600,"Empty input,defaults to 3600 seconds")  # dupa tama

    date = datetime.today()
    input_date = input_date.strip()

    time_measure = input_date[-1]
    try:
        if time_measure not in TIME_SUFFIXES:
            raise ValueError
        interval   = int(input_date[:-1])
    except ValueError:
        time_measure = 's'
        interval = 3600

    to_date = None

    if time_measure == 's':
        to_date = date
        from_date = to_date - timedelta(seconds=interval)
    elif time_measure == 'm':
        to_date = datetime(year=date.year, month=date.month, day=date.day, hour=date.hour, minute=date.minute,second=0)
        from_date = to_date - timedelta(minutes=interval)
    elif time_measure == 'h':
        to_date = datetime(year=date.year, month=date.month, day=date.day, hour=date.hour)
        from_date = to_date - timedelta(hours=interval)
    elif time_measure == 'D':
        to_date =  datetime(year=date.year, month=date.month, day=date.day)
        from_date= to_date - timedelta(days=interval)
#    elif time_measure == 'M':
#        to_date = datetime(year=date.year, month=date.month,day=1)
#        from_date = to_date - timedelta(days=interval*DAYS_IN_MONTH)
#    elif time_measure == 'Y':
#        to_date = datetime(year=date.year,month=1,day=1)
#        from_date = to_date - timedelta(days=interval*DAYS_IN_Y)

    to_str   = "to_date('%s','%s')" % ( to_date.strftime("%Y-%m-%d %H:%M:%S"), DATE_FORMAT)
    from_str = "to_date('%s','%s')" % ( from_date.strftime("%Y-%m-%d %H:%M:%S"), DATE_FORMAT)

    return (from_str, to_str)

#print convert_to_seconds("")
#f,t= calculate_time_ranges("2D")
#print f,t
